﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Xml;
using Blackbird.Common.Properties;
using Blackbird.Common.Utility;
using Blackbird.Core.Editable.Base;

namespace Blackbird.Core.Editable.Map
{
    public class EditableLayeredMap : EditableEntityCollection<EditableMapLayer>
    {
        private int _height;
        /// <summary>
        /// Gets the Height property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public int Height {
            get { return _height; }
            set {
                if (_height == value) {
                    return;
                }
                _height = value;
                InvokePropertyChanged("Height");
            }
        }
        
        private int _width;
        /// <summary>
        /// Gets the Width property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public int Width {
            get { return _width; }
            set {
                if (_width == value) {
                    return;
                }
                _width = value;
                InvokePropertyChanged("Width");
            }
        }

        public override EditableMapLayer NewChild(object param)
        {
            var newLayer = (param as EditableMapLayer) ?? new EditableMapLayer(Width, Height);
            newLayer.Name = string.Format(Resources.UntitledEntityFormat, newLayer.TypeName);
            Add(newLayer);
            return newLayer;
        }

        public void Sort()
        {
            // HACK: I'm a cheater
            var sortList = new List<EditableMapLayer>(Items);
            sortList.Sort();
            Items = new ObservableCollection<EditableMapLayer>(sortList);
        }

        public EditableMapLayer Layer(int layerIndex)
        {
            return Items.SingleOrDefault(map => map.LayerIndex == layerIndex);
        }

        protected override void ReadXmlContent(XmlReader reader)
        {
            Items.Clear();

            Name = SerializationUtility.ReadAttribute(reader, "name", "name");
            reader.ReadStartElement("layers");
            reader.MoveToContent();
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                var map = new EditableMapLayer();
                map.ReadXml(reader);
                Items.Add(map);
                reader.Read();
            }
        }

        protected override void WriteXmlContent(XmlWriter writer)
        {
            writer.WriteAttributeString("name", Name);
            writer.WriteStartElement("layers");
            foreach (EditableMapLayer layer in Items)
            {
                layer.WriteXml(writer);
            }
            writer.WriteEndElement();
        }

        public override string TypeName
        {
            get { return Resources.EditableLayedMapTypeFriendlyName; }
        }

        protected override void ReadXmlAttributes(XmlReader reader)
        {
            Width = SerializationUtility.ReadAttribute(reader, "width", 1);
            Height = SerializationUtility.ReadAttribute(reader, "height", 1);
        }

        protected override void WriteXmlAttributes(XmlWriter writer)
        {
            writer.WriteAttributeString("width", Width.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("height", Height.ToString(CultureInfo.CurrentCulture));
        }
    }
}